package io.afu.nginxhelper.dto.common.upstream;

import com.sun.org.apache.regexp.internal.RE;
import io.afu.nginxhelper.constant.NgxConst;
import io.afu.nginxhelper.utils.StringUtils;

public class UpServer {
    public static final String KEY = "server";

    private final String DOWN = "down";

    private final String RESOLV = "resolv";

    private final String BACKUP = "backup";

    private final String FAIL_TIMEOUT = "fail_timeout";

    private final String MAX_FAILS = "max_fails";

    private final String WEIGHT = "weight";

    private final String MAX_CONNS = "max_conns";

    private final String SERVICE = "service";

    private final String ROUTE = "route";

    private String server;

    private boolean down = false;

    private boolean resolv = false;

    private boolean baskup = false;

    private String failTimeout;

    private String maxFails;

    private String weight;

    private String maxConns;

    private String service;

    private String route;

    public void parse(String line) {

    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(UpServer.KEY).append(NgxConst.SEP);
        sb.append(server).append(NgxConst.SEP);
        if (down){
            sb.append(DOWN).append(NgxConst.SEP);
        }
        if (resolv){
            sb.append(RESOLV).append(NgxConst.SEP);
        }
        if (baskup){
            sb.append(BACKUP).append(NgxConst.SEP);
        }
        if (StringUtils.isNotEmpty(failTimeout)){
            sb.append(FAIL_TIMEOUT).append(NgxConst.EQUAL).append(failTimeout).append(NgxConst.SEP);
        }
        if (StringUtils.isNotEmpty(maxFails)){
            sb.append(MAX_FAILS).append(NgxConst.EQUAL).append(maxFails).append(NgxConst.SEP);
        }
        if (StringUtils.isNotEmpty(weight)){
            sb.append(WEIGHT).append(NgxConst.EQUAL).append(weight).append(NgxConst.SEP);
        }
        if (StringUtils.isNotEmpty(maxConns)){
            sb.append(MAX_CONNS).append(NgxConst.EQUAL).append(maxConns).append(NgxConst.SEP);
        }
        if (StringUtils.isNotEmpty(service)){
            sb.append(SERVICE).append(NgxConst.EQUAL).append(service).append(NgxConst.SEP);
        }
        if (StringUtils.isNotEmpty(route)){
            sb.append(ROUTE).append(NgxConst.EQUAL).append(route).append(NgxConst.SEP);
        }
        sb.append(NgxConst.END_MARK).append(NgxConst.NEW_LINE);
        return sb.toString();
    }
}
